package org.jivesoftware.smack.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
import org.jivesoftware.smack.util.dns.DNSResolver;
import org.jivesoftware.smack.util.dns.HostAddress;
import org.jivesoftware.smack.util.dns.SRVRecord;

public class DNSUtil
{
  private static Map<String, List<HostAddress>> cache = new Cache(100, 600000L);
  private static DNSResolver dnsResolver = null;

  public static void setDNSResolver(DNSResolver paramDNSResolver)
  {
    dnsResolver = paramDNSResolver;
  }

  public static DNSResolver getDNSResolver()
  {
    return dnsResolver;
  }

  public static List<HostAddress> resolveXMPPDomain(String paramString)
  {
    return resolveDomain(paramString, 'c');
  }

  public static List<HostAddress> resolveXMPPServerDomain(String paramString)
  {
    return resolveDomain(paramString, 's');
  }

  private static List<HostAddress> resolveDomain(String paramString, char paramChar)
  {
    String str1 = paramChar + paramString;
    if (cache.containsKey(str1))
    {
      localObject = (List)cache.get(str1);
      if (localObject != null)
        return localObject;
    }
    if (dnsResolver == null)
      throw new IllegalStateException("No DNS resolver active.");
    Object localObject = new ArrayList();
    String str2;
    if (paramChar == 's')
      str2 = "_xmpp-server._tcp." + paramString;
    else if (paramChar == 'c')
      str2 = "_xmpp-client._tcp." + paramString;
    else
      str2 = paramString;
    List localList1 = dnsResolver.lookupSRVRecords(str2);
    List localList2 = sortSRVRecords(localList1);
    if (localList2 != null)
      ((List)localObject).addAll(localList2);
    ((List)localObject).add(new HostAddress(paramString));
    cache.put(str1, localObject);
    return localObject;
  }

  protected static List<HostAddress> sortSRVRecords(List<SRVRecord> paramList)
  {
    if ((paramList.size() == 1) && (((SRVRecord)paramList.get(0)).getFQDN().equals(".")))
      return null;
    Collections.sort(paramList);
    TreeMap localTreeMap = new TreeMap();
    Object localObject2 = paramList.iterator();
    Object localObject4;
    while (((Iterator)localObject2).hasNext())
    {
      localObject1 = (SRVRecord)((Iterator)localObject2).next();
      localObject3 = Integer.valueOf(((SRVRecord)localObject1).getPriority());
      localObject4 = (List)localTreeMap.get(localObject3);
      if (localObject4 == null)
      {
        localObject4 = new LinkedList();
        localTreeMap.put(localObject3, localObject4);
      }
      ((List)localObject4).add(localObject1);
    }
    Object localObject1 = new ArrayList(paramList.size());
    Object localObject3 = localTreeMap.keySet().iterator();
    while (((Iterator)localObject3).hasNext())
    {
      localObject2 = (Integer)((Iterator)localObject3).next();
      localObject4 = (List)localTreeMap.get(localObject2);
      int i;
      while ((i = ((List)localObject4).size()) > 0)
      {
        int[] arrayOfInt = new int[((List)localObject4).size()];
        int j = 0;
        int k = 0;
        int m = 1;
        Iterator localIterator = ((List)localObject4).iterator();
        SRVRecord localSRVRecord1;
        while (localIterator.hasNext())
        {
          localSRVRecord1 = (SRVRecord)localIterator.next();
          if (localSRVRecord1.getWeight() > 0)
            m = 0;
        }
        localIterator = ((List)localObject4).iterator();
        while (localIterator.hasNext())
        {
          localSRVRecord1 = (SRVRecord)localIterator.next();
          j += localSRVRecord1.getWeight() + m;
          arrayOfInt[k] = j;
          k++;
        }
        int n;
        if (j == 0)
        {
          n = (int)(Math.random() * i);
        }
        else
        {
          double d = Math.random() * j;
          n = bisect(arrayOfInt, d);
        }
        SRVRecord localSRVRecord2 = (SRVRecord)((List)localObject4).remove(n);
        ((List)localObject1).add(localSRVRecord2);
      }
    }
    return localObject1;
  }

  private static int bisect(int[] paramArrayOfInt, double paramDouble)
  {
    int i = 0;
    int[] arrayOfInt = paramArrayOfInt;
    int m = paramArrayOfInt.length;
    for (int k = 0; k < m; k++)
    {
      int j = arrayOfInt[k];
      if (paramDouble < j)
        break;
      i++;
    }
    return i;
  }
}

/* Location:           D:\hygieia\userclient\common\libs\easemobchat_2.1.8.jar
 * Qualified Name:     org.jivesoftware.smack.util.DNSUtil
 * JD-Core Version:    0.6.1
 */